﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// search_photo 的摘要说明
    /// </summary>
    public class search_photo : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        public void ProcessRequest(HttpContext context)
        {
            string keyword = Utils.ToHtml(DTRequest.GetQueryString("keyword"));

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取图库信息
            BLL.photos qz_bll = new BLL.photos();
            Model.photos model = qz_bll.GetModel(keyword);

            if (model != null)
            {
                //循环图片列表
                if (model.albums != null)
                {
                    strTxt.Append("\"pic_list\":[");
                    foreach (Model.photo_albums modelt in model.albums)
                    {
                        strTxt.Append("{");
                        strTxt.Append("\"id\":" + modelt.id);
                        if (modelt.original_path + "" != "")
                        {
                            if (modelt.original_path.Contains("shicaiquanzi"))
                            {
                                if (modelt.original_path.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + modelt.original_path + "\""); }
                                else { strTxt.Append(",\"img_url\":\"http://" + modelt.original_path + "\""); }

                            }
                            else
                            {
                                strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + modelt.original_path + "\"");
                            }
                        }
                        else { strTxt.Append(",\"img_url\":\"\""); }
                       
                        //strTxt.Append(",\"img_url\":\"http://" + modelt.original_path + "\"");
                        strTxt.Append("},");
                    }
                    strTxt.Remove(strTxt.ToString().LastIndexOf(','), 1);
                    strTxt.Append("]");
                }
                else
                {
                    strTxt.Append("\"pic_list\":[]");
                }
                strTxt.Append(",\"photo_info\":\"" + Microsoft.JScript.GlobalObject.escape(Utils.ClearHtmlExceptA(model.content)) + "\"");
            }
            else
            {
                strTxt.Append("\"pic_list\":[]");
                strTxt.Append(",\"photo_info\":[]");
            }
            
            //获取圈子信息
            Model.quanzi qzModel = new BLL.quanzi().GetModel(keyword);
            if (qzModel != null)
            {
                strTxt.Append(",\"quanzi_id\":" + qzModel.id);
                strTxt.Append(",\"quanzi_title\":\"" + qzModel.title + "\"");
            }
            else
            {
                strTxt.Append(",\"quanzi_id\":0");
                strTxt.Append(",\"quanzi_title\":\"\"");
            }

            //获取价格行情
            //BLL.hangqing_price bll_h = new BLL.hangqing_price();
            //DataSet ds_h = bll_h.GetAllList(keyword);
            ////如果记录存在
            //if (ds_h.Tables[0].Rows.Count > 0)
            //{
            //    strTxt.Append(",\"hangqing_list\":[");
            //    for (int i = 0; i < ds_h.Tables[0].Rows.Count; i++)
            //    {
            //        DataRow dr_h = ds_h.Tables[0].Rows[i];
            //        strTxt.Append("{");
            //        strTxt.Append("\"id\":" + dr_h["id"]);
            //        string shicainame = new BLL.hangqing().GetTitle(int.Parse(dr_h["article_id"].ToString()));
            //        string _title = dr_h["title"].ToString();
            //        if (_title.Length > 7)
            //        {
            //            strTxt.Append(",\"title\":\"" + shicainame + " " + _title.Substring(0, 7) + "\"");
            //        }
            //        else
            //        {
            //            strTxt.Append(",\"title\":\"" + shicainame + " " + _title + "\"");
            //        }
            //        strTxt.Append(",\"price\":\"" + dr_h["price"] + "\"");
            //        strTxt.Append(",\"is_up\":" + dr_h["is_up"]);
            //        strTxt.Append("}");
            //        //是否加逗号
            //        if (i < ds_h.Tables[0].Rows.Count - 1)
            //        {
            //            strTxt.Append(",");
            //        }
            //    }
            //    strTxt.Append("]");
            //}
            //else
            //{
            //    strTxt.Append(",\"hangqing_list\":[]");
            //}
            strTxt.Append(",\"hangqing_list\":[]");
            //求购list
            //BLL.qiugou bll2 = new BLL.qiugou();
            //DataSet ds2 = bll2.GetList(10, "title like '%" + keyword + "%' or content like '%" + keyword + "%'", "id desc");
            ////如果记录存在
            //if (ds2.Tables[0].Rows.Count > 0)
            //{
            //    strTxt.Append(",\"qiugou_list\":[");
            //    for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            //    {
            //        DataRow dr = ds2.Tables[0].Rows[i];
            //        strTxt.Append("{");
            //        strTxt.Append("\"id\":" + dr["id"]);
            //        strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
            //        strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");
            //        string _img_url = dr["img_url"].ToString();
            //        if (string.IsNullOrEmpty(_img_url))
            //        {
            //            strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
            //        }
            //        else if (_img_url.Contains("shicaiquanzi"))
            //        {
            //            strTxt.Append(",\"img_url\":\"http://" + _img_url + "\"");
            //        }
            //        else
            //        {
            //            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + _img_url + "\"");
            //        }
            //        strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
            //        strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
            //        strTxt.Append("}");
            //        //是否加逗号
            //        if (i < ds2.Tables[0].Rows.Count - 1)
            //        {
            //            strTxt.Append(",");
            //        }
            //    }
            //    strTxt.Append("]");
            //}
            //else
            //{
            //    strTxt.Append(",\"qiugou_list\":[]");
            //}
            strTxt.Append(",\"qiugou_list\":[]");
            ////获取供应信息
            //BLL.gongying bll5 = new BLL.gongying();
            //DataSet ds5 = bll5.GetList(10, "title like '%" + keyword + "%' or content like '%" + keyword + "%' and status=1", "id desc");
            ////如果记录存在
            //if (ds5.Tables[0].Rows.Count > 0)
            //{
            //    strTxt.Append(",\"gongying_list\":[");
            //    for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
            //    {
            //        DataRow dr5 = ds5.Tables[0].Rows[i];
            //        strTxt.Append("{");
            //        strTxt.Append("\"id\":" + dr5["id"]);
            //        strTxt.Append(",\"title\":\"" + dr5["title"] + "\"");
            //        strTxt.Append(",\"size\":\"" + dr5["size_long"] + "×" + dr5["size_wide"] + "×" + dr5["size_height"] + "\"");

            //        string gongying_img = dr5["img_url"].ToString();
            //        if (string.IsNullOrEmpty(gongying_img))
            //        {
            //            strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
            //        }
            //        else if (gongying_img.Contains("shicaiquanzi"))
            //        {
            //            strTxt.Append(",\"img_url\":\"http://" + gongying_img + "\"");
            //        }
            //        else
            //        {
            //            strTxt.Append(",\"img_url\":\"http:\\/\\/pic.shicaiquanzi.com" + gongying_img + "\"");
            //        }
            //        strTxt.Append(",\"lianxiren\":\"" + dr5["lianxiren"] + "\"");
            //        strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr5["add_time"])) + "\"");
            //        strTxt.Append("}");
            //        //是否加逗号
            //        if (i < ds5.Tables[0].Rows.Count - 1)
            //        {
            //            strTxt.Append(",");
            //        }
            //    }
            //    strTxt.Append("]");
            //}
            //else
            //{
            //    strTxt.Append(",\"gongying_list\":[]");
            //}
            strTxt.Append(",\"gongying_list\":[]");

            //获取企业信息
            BLL.company qg_bll = new BLL.company();
            DataSet qg_ds = qg_bll.GetList(10,1, "title like '%" + keyword + "%' or pro_remark like '%" + keyword + "%' and status=1", "sort_id desc,group_id desc,id asc", out this.totalCount);
            //如果记录存在
            int xx_total_page = this.totalCount / 10;
            strTxt.Append(",\"total_page\":" + xx_total_page.ToString());
            if (qg_ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append(",\"company_list\":[");
                for (int i = 0; i < qg_ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = qg_ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    if (dr["logo"].ToString().Contains("shicaiquanzi"))
                    {
                        if (dr["logo"].ToString().Contains("http://")) {  strTxt.Append(",\"img_url\":\"" + dr["logo"] + "\""); }
                        else {  strTxt.Append(",\"img_url\":\"http://" + dr["logo"] + "\"");  }
                    }
                    else
                    {
                        strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + dr["logo"] + "\""); 
                    }
                    //strTxt.Append(",\"img_url\":\"http://" + dr["logo"] + "\"");
                    strTxt.Append(",\"pro_remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr["pro_remark"].ToString()) + "\"");
                    strTxt.Append(",\"group_id\":" + dr["group_id"]);
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < qg_ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                strTxt.Append(",\"company_list\":[]");
            }
           
           
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}